package com.siwei.mes.service.system.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.ProjectArea;
import com.siwei.mes.entity.system.User;
import com.siwei.mes.mapper.system.ProjectAreaMapper;
import com.siwei.mes.mapper.system.UserMapper;
import com.siwei.mes.service.system.ProjectAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 用户表(User)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 18:16:11
 */
@Service
public class ProjectAreaServiceImpl implements ProjectAreaService {
    @Resource
    private ProjectAreaMapper projectAreaMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Long create(ProjectArea entity) {
        entity.init(true);
        assertTrue(null != projectAreaMapper.selectByProjectArea(entity.getProjectArea()), "区域名称已经存在");
        projectAreaMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ProjectArea entity) {
        entity.init(false);
        ProjectArea user = projectAreaMapper.selectByPrimaryKey(entity.getId());
        assertTrue(user == null, "工程区域划分信息不存在");
        projectAreaMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ProjectArea user = projectAreaMapper.selectByPrimaryKey(id);
        assertTrue(user == null, "工程区域划分不存在");
        projectAreaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ProjectArea> getPage(PageQuery<ProjectArea, ProjectArea> query) {
        ProjectArea searchParam = query.getParams();
        PageInfo<ProjectArea> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> projectAreaMapper.getList(searchParam));
        for (ProjectArea projectArea : pageInfo.getList()) {
            if (StringUtils.isNotBlank(projectArea.getHeadmanId())) {
                String headmanId = projectArea.getHeadmanId();
                User user = userMapper.selectByPrimaryKey(headmanId);
                if (user != null) {
                    projectArea.setHeadmanName(user.getUserName());
                }
            }
            if (StringUtils.isNotBlank(projectArea.getCustomerServiceId())) {
                List<String> customerServiceIds = Arrays.asList(projectArea.getCustomerServiceId().split(","));
                List<User> userList = userMapper.getList(new User().setIds(customerServiceIds));
                if (CollectionUtil.isNotEmpty(userList)) {
                    projectArea.setCustomerServiceName(userList.stream().map(User::getUserName).collect(Collectors.joining(",")));
                }
            }
        }
        return pageInfo;
    }

    @Override
    public ProjectArea loadById(Long id) {
        /*ProjectArea projectArea = projectAreaMapper.selectByPrimaryKey(id);
        List<ProjectArea> list = projectAreaMapper.getList(new ProjectArea().setHeadmanId(projectArea.getHeadmanId()));
        if(!CollectionUtils.isEmpty(list)){
            List<String> customerServiceId = new ArrayList<>();
            List<String> customerServiceName = new ArrayList<>();
            for (ProjectArea project :list){
                customerServiceId.addAll(Arrays.asList(project.getCustomerServiceId().split(",")));
                customerServiceName.addAll(Arrays.asList(project.getCustomerServiceName().split(",")));
            }
            projectArea.setCustomerServiceId(String.join(",",customerServiceId.stream()
                    .distinct()
                    .collect(Collectors.toList())));
            projectArea.setCustomerServiceName(String.join(",",customerServiceName.stream()
                    .distinct()
                    .collect(Collectors.toList())));
        }*/
        return projectAreaMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ProjectArea> getAll(ProjectArea entity) {
        return projectAreaMapper.getList(entity);
    }

    @Override
    public void saveOrUpdate(String projectArea) {
        if (StringUtils.isBlank(projectArea)) {
            return;
        }
        ProjectArea entity = projectAreaMapper.selectByProjectArea(projectArea);
        if (entity == null) {
            ProjectArea insertInfo = new ProjectArea();
            insertInfo.setProjectArea(projectArea);
            insertInfo.init(true);
            projectAreaMapper.insertSelective(insertInfo);
        }
    }
}
